using System;
using System.Collections.Generic;
using System.Reflection;
using Common.Core;

namespace Common.Unity.Extensions.PropertyMapper
{
	public static class SimilarPropertyHelper
	{
		public static IEnumerable<string> ResolveSimiliarProperties( PropertyMappingKey key, bool includeNonPublicProperties )
		{
			foreach ( PropertyInfo info in includeNonPublicProperties ? key.TargetBuildKey.Type.GetProperties( CommonBindingFlags.AllProperties ) : key.TargetBuildKey.Type.GetProperties() )
			{
				if ( info.CanRead && info.CanWrite )
				{
					PropertyInfo sourceInfo = includeNonPublicProperties
					                          	? key.Source.GetProperty( info.Name, CommonBindingFlags.AllProperties )
					                          	: key.Source.GetProperty( info.Name );
					if ( sourceInfo != null && sourceInfo.CanRead && sourceInfo.CanWrite && ConversionHelper.IsConvertable( sourceInfo.PropertyType, info.PropertyType ) )
					{
						yield return sourceInfo.Name;
					}
				}
			}	
		}
	}
}